package com.itheima.health.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @program: itcast_health
 * @create: 2019-10-18 15:48
 * @author: li he
 * @description:
 */
@Service
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    @Transactional
    public void add(CheckItem checkItem) {
        System.out.println("CheckItemServiceImpl.add");
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //使用分页插件PageHelper
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

        Page<CheckItem> page= checkItemDao.findByCondition(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(), page );
    }

    @Override
    @Transactional
    public void delById(int id) {
        long count =  checkItemDao.findCountById(id);
        if (count > 0){ //被关联 不能删除
            throw new RuntimeException("该检查项被检查组关联, 不能被删除 ! ! ! ");
        } else { // 可以删除
            checkItemDao.delById(id);
        }
    }

    @Override
    public CheckItem findById(int id) {
        return checkItemDao.findById(id);
    }

    @Override
    @Transactional
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    @Override
    public long findTotal() {
        return checkItemDao.findTotal();
    }

    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }


}